﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace ShortcutShow
{

    class Startup
    {
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        static List<String> buffer;
        static List<String> holding;
        static bool canI = true;
        private static string S2F;
        static MainWindow window;
        static App app;

        [STAThread]
        static void Main()
        {
            buffer = new List<string>();
            holding = new List<string>();
            app = new App();
            app.MainWindow = new settingwindows();
            app.MainWindow.Show();
            window = new MainWindow();
            window.Show();
            
            //ConsoleHelper.Create();
            //app.Run();
            //var handle = GetConsoleWindow();

            

            // Hide
            //ShowWindow(handle, SW_HIDE);

            _hookID = SetHook(_proc);
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }

        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_HIDE = 0;

        private static void add2buffer(String k)
        {

            if (!isThisHolding(k))
            {
                if (buffer.Count == 0)
                {
                    Keys kk;
                    Enum.TryParse<Keys>(k, out kk);
                    if (canBeFirst(kk))
                    {
                        holding.Add(k);
                        buffer.Add(normalize(k));
                    }
                }
                else
                {
                    holding.Add(k);
                    buffer.Add(normalize(k));
                }
            }

        }

        private static void deleteBuffer()
        {
            foreach (String item in buffer)
            {
                Console.Write(item);
            } Console.WriteLine();

            String a = "";
            foreach (String item in buffer)
            {
                a += item + " ";
            }

            //app = new App();

            window.refreshSettings();
            window.makeVisible(a);
            
            buffer.Clear();
            canI = true;
            holding.Clear();

            
        }

        private static bool canBeFirst(Keys k)
        {
            if (k == Keys.RControlKey || k == Keys.RShiftKey || k == Keys.LControlKey || k == Keys.LShiftKey || k == Keys.LMenu || k == Keys.RMenu || k == Keys.LWin || k == Keys.Escape || k == Keys.F1 || k == Keys.F2 || k == Keys.F3 || k == Keys.F4 || k == Keys.F5 || k == Keys.F6 || k == Keys.F7 || k == Keys.F8 || k == Keys.F9 || k == Keys.F10 || k == Keys.F11 || k == Keys.F12) return true;
            else return false;
        }

        private static String normalize(String k)
        {
            String r = "";

            Keys kk;
            Enum.TryParse<Keys>(k, out kk);

            switch (kk)
            {
                case Keys.Add:
                    r = "+";
                    break;
                case Keys.Subtract:
                    r = "-";
                    break;
                case Keys.LControlKey:
                    r = "CTRL";
                    break;
                case Keys.LShiftKey:
                    r = "SHIFT";
                    break;
                case Keys.LMenu:
                    r = "ALT";
                    break;
                case Keys.RControlKey:
                    r = "CTRL";
                    break;
                case Keys.RShiftKey:
                    r = "SHIFT";
                    break;
                case Keys.RMenu:
                    r = "ALT";
                    break;
                case Keys.Oem1:
                    r = ";";
                    break;
                case Keys.Oem5:
                    r = "\\";
                    break;
                case Keys.Oem6:
                    r = "]";
                    break;
                case Keys.Oem7:
                    r = "'";
                    break;
                case Keys.OemBackslash:
                    r = "\\";
                    break;
                case Keys.OemMinus:
                    r = "-";
                    break;
                case Keys.OemOpenBrackets:
                    r = "[";
                    break;
                case Keys.OemPeriod:
                    r = ".";
                    break;
                case Keys.OemQuestion:
                    r = "/";
                    break;
                case Keys.Oemcomma:
                    r = ",";
                    break;
                case Keys.Oemplus:
                    r = "=";
                    break;
                case Keys.Oemtilde:
                    r = "`";
                    break;
                case Keys.D0:
                    r = ")";
                    break;
                case Keys.D1:
                    r = "!";
                    break;
                case Keys.D2:
                    r = "@";
                    break;
                case Keys.D3:
                    r = "#";
                    break;
                case Keys.D4:
                    r = "$";
                    break;
                case Keys.D5:
                    r = "%";
                    break;
                case Keys.D6:
                    r = "^";
                    break;
                case Keys.D7:
                    r = "&";
                    break;
                case Keys.D8:
                    r = "*";
                    break;
                case Keys.D9:
                    r = "(";
                    break;
                case Keys.NumPad0:
                    r = "0";
                    break;
                case Keys.NumPad1:
                    r = "1";
                    break;
                case Keys.NumPad2:
                    r = "2";
                    break;
                case Keys.NumPad3:
                    r = "3";
                    break;
                case Keys.NumPad4:
                    r = "4";
                    break;
                case Keys.NumPad5:
                    r = "5";
                    break;
                case Keys.NumPad6:
                    r = "6";
                    break;
                case Keys.NumPad7:
                    r = "7";
                    break;
                case Keys.NumPad8:
                    r = "8";
                    break;
                case Keys.NumPad9:
                    r = "9";
                    break;
                case Keys.Divide:
                    r = "/";
                    break;
                case Keys.Multiply:
                    r = "*";
                    break;
                case Keys.LWin:
                    r = "WIN";
                    break;
                case Keys.Return:
                    r = "ENTER";
                    break;
                case Keys.Left:
                    r = "←";
                    break;
                case Keys.Right:
                    r = "→";
                    break;
                case Keys.Up:
                    r = "↑";
                    break;
                case Keys.Down:
                    r = "↓";
                    break;
                case Keys.Escape:
                    r = "ESC";
                    break;
                default:
                    r = kk.ToString();
                    break;
            }

            return r;
        }

        private delegate IntPtr LowLevelKeyboardProc(
            int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            //Console.WriteLine(wParam);

            if (nCode >= 0 && (wParam == (IntPtr)260))
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Keys k = (Keys)vkCode;
                //Console.WriteLine(k.ToString());
                add2buffer(k.ToString());

            }
            if (nCode >= 0 && (wParam == (IntPtr)261))
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Keys k = (Keys)vkCode;

                if (isThisHolding(k.ToString()))
                    removeHold(k.ToString());

                if (holding.Count == 0 && buffer.Count > 0)
                    deleteBuffer();

            }


            if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN))
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Keys k = (Keys)vkCode;

                add2buffer(k.ToString());

            }
            if (nCode >= 0 && (wParam == (IntPtr)WM_KEYUP))
            {

                int vkCode = Marshal.ReadInt32(lParam);
                Keys k = (Keys)vkCode;
                if (isThisHolding(k.ToString()))
                    removeHold(k.ToString());

                if (holding.Count == 0 && buffer.Count > 0)
                    deleteBuffer();

            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private static bool isThisHolding(string k)
        {
            if (holding.Count > 0)
            {
                S2F = k.ToString();
                int i = 0;
                i = holding.FindLastIndex(isThisIt);
                if (i == -1)
                {
                    return false;
                }
                return true;
            } return false;
        }

        private static void removeHold(String k)
        {
            S2F = k.ToString();
            int i = 0;
            i = holding.FindLastIndex(isThisIt);
            if (i != -1)
            {
                holding.RemoveAt(i);

            }
        }

        private static bool isThisIt(String s)
        {
            if (s == S2F) return true;
            else return false;
        }
    }
}
